package com.cnpinxi.service;

import com.cnpinxi.domain.Order;
import com.cnpinxi.domain.PayParameter;
import com.cnpinxi.domain.Payment;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Baggio on 2018-3-7.
 */
@Service("conversionService")
public class ConversionPayServiceImpl implements PayService {
    @Autowired OrderService orderService;
    @Autowired PaymentService paymentService;
    @Override
    public PayParameter pay(String orderId, String price, String remoteAddr, Map<String, String> payParametersMap) throws Exception {
        PayParameter payParameter=new PayParameter();
       Order order=orderService.getOrderById(Long.parseLong(orderId));
       if(order==null){
           throw new MyRunTimeExcption("订单不存在!");
       }
       if(order.getStatus()!=OrderService.UNPAY){
           throw new MyRunTimeExcption("订单状态不能支付!");
       }
       Map<String,String> map=new HashMap<>();
       map.put("orderId",orderId);
       map.put("payMode",String.valueOf(Payment.PAY_MODE_CONVERSION));
        Payment payment=paymentService.selectPayment(map);

        if(payment==null){
            throw new MyRunTimeExcption("支付项目不存在！");
        }

        if(payment.getStatus()==OrderService.PAYED){
            throw new MyRunTimeExcption("已经支付过了！，不用再次支付！");
        }

        payment.setStatus(OrderService.PAYED);


        paymentService.modifyPayment(payment);
        payParameter.setPayStatus(PayParameter.PAY_STATUS_PAYED);
        payParameter.setPayMode(Payment.PAY_MODE_CONVERSION);
        return payParameter;
    }

    @Override
    public Map<String, String> notification(Map<String, String> map) {
        return null;
    }

    @Override
    public PayParameter refund(Order order, Integer price) {
        return null;
    }
}
